package com.liuyongsheng.teafile.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web配置类
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${file.storage.root-path}")
    private String storagePath;

    /**
     * 配置资源处理器
     * @param registry 资源处理器注册表，用于注册静态资源处理器
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置Swagger UI静态资源映射
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        // 配置按年月存储的文件访问路径映射
        registry.addResourceHandler("/**")
                .addResourceLocations("file:" + storagePath + "/")
                .addResourceLocations("classpath:/static/");
    }
}
